package com.fr.base.background;

import com.fr.base.BaseUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.serializable.ImageSerializable;
import com.fr.base.core.serializable.ShapeSerializable;
import com.fr.chart.core.ChartUtils;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TextureBackground extends AbstractBackground
{
  private static final long serialVersionUID = 1994011909459388705L;
  private static final Rectangle2D embededImageRect = new Rectangle2D.Double(0.0D, 0.0D, 58.0D, 42.0D);
  private transient TexturePaint texturePaint;
  public static final TexturePaint NewsprintTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Newsprint.gif"), embededImageRect);
  public static final TexturePaint RecycledPaperTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/RecycledPaper.gif"), embededImageRect);
  public static final TexturePaint ParchmentTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Parchment.gif"), embededImageRect);
  public static final TexturePaint StationeryTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Stationery.gif"), embededImageRect);
  public static final TexturePaint GreenMarbleTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/GreenMarble.gif"), embededImageRect);
  public static final TexturePaint WhiteMarbleTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/WhiteMarble.gif"), embededImageRect);
  public static final TexturePaint BrownMarbleTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/BrownMarble.gif"), embededImageRect);
  public static final TexturePaint GraniteTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Granite.gif"), embededImageRect);
  public static final TexturePaint BlueTissuePaperTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/BlueTissuePaper.gif"), embededImageRect);
  public static final TexturePaint PinkTissuePaperTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/PinkTissuePaper.gif"), embededImageRect);
  public static final TexturePaint PurpleMeshTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/PurpleMesh.gif"), embededImageRect);
  public static final TexturePaint BouquetTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Bouquet.gif"), embededImageRect);
  public static final TexturePaint PapyrusTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Papyrus.gif"), embededImageRect);
  public static final TexturePaint CanvasTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Canvas.gif"), embededImageRect);
  public static final TexturePaint DenimTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Denim.gif"), embededImageRect);
  public static final TexturePaint WovenMatTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/WovenMat.gif"), embededImageRect);
  public static final TexturePaint WaterDropletsTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/WaterDroplets.gif"), embededImageRect);
  public static final TexturePaint PaperBagTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/PaperBag.gif"), embededImageRect);
  public static final TexturePaint FishFossilTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/FishFossil.gif"), embededImageRect);
  public static final TexturePaint SandTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Sand.gif"), embededImageRect);
  public static final TexturePaint CorkTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Cork.gif"), embededImageRect);
  public static final TexturePaint WalnutTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Walnut.gif"), embededImageRect);
  public static final TexturePaint OakTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/Oak.gif"), embededImageRect);
  public static final TexturePaint MediumWoodTexturePaint = new TexturePaint(createBufferedImage("/com/fr/base/background/images/MediumWood.gif"), embededImageRect);

  public TextureBackground()
  {
    this(null);
  }

  public TextureBackground(TexturePaint paramTexturePaint)
  {
    this.texturePaint = null;
    setTexturePaint(paramTexturePaint);
  }

  private static BufferedImage createBufferedImage(String paramString)
  {
    return GraphHelper.createBufferedImageFromImage(BaseUtils.readImage(paramString));
  }

  public void paint(Graphics paramGraphics, Shape paramShape)
  {
    Graphics2D localGraphics2D1 = (Graphics2D)paramGraphics;
    if (getTexturePaint() == null)
      return;
    Rectangle2D localRectangle2D1 = paramShape.getBounds2D();
    BufferedImage localBufferedImage1 = GraphHelper.createBufferedImage((int)localRectangle2D1.getWidth(), (int)localRectangle2D1.getHeight(), 2);
    Graphics2D localGraphics2D2 = localBufferedImage1.createGraphics();
    BufferedImage localBufferedImage2 = getTexturePaint().getImage();
    Rectangle2D localRectangle2D2 = getTexturePaint().getAnchorRect();
    int i = (int)localRectangle2D2.getWidth();
    int j = (int)localRectangle2D2.getHeight();
    if ((i > 0) && (j > 0))
    {
      int k = 0;
      while (k < localRectangle2D1.getHeight())
      {
        int l = 0;
        while (l < localRectangle2D1.getWidth())
        {
          localGraphics2D2.drawImage(localBufferedImage2, l, k, i, j, null);
          l += i;
        }
        k += j;
      }
    }
    localGraphics2D2.dispose();
    Shape localShape = localGraphics2D1.getClip();
    localGraphics2D1.clip(paramShape);
    localGraphics2D1.translate(localRectangle2D1.getX(), localRectangle2D1.getY());
    localGraphics2D1.drawImage(localBufferedImage1, 0, 0, null);
    localGraphics2D1.translate(-localRectangle2D1.getX(), -localRectangle2D1.getY());
    localGraphics2D1.setClip(localShape);
  }

  public TexturePaint getTexturePaint()
  {
    return this.texturePaint;
  }

  public void setTexturePaint(TexturePaint paramTexturePaint)
  {
    this.texturePaint = paramTexturePaint;
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws ClassNotFoundException, IOException
  {
    paramObjectInputStream.defaultReadObject();
    ShapeSerializable localShapeSerializable = (ShapeSerializable)paramObjectInputStream.readObject();
    Rectangle2D localRectangle2D = (Rectangle2D)localShapeSerializable.getShape();
    BufferedImage localBufferedImage = null;
    Object localObject = paramObjectInputStream.readObject();
    if (localObject != null)
      localBufferedImage = GraphHelper.createBufferedImageFromImage(((ImageSerializable)localObject).getImage());
    this.texturePaint = new TexturePaint(localBufferedImage, localRectangle2D);
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
    if (this.texturePaint == null)
      return;
    Rectangle2D localRectangle2D = this.texturePaint.getAnchorRect();
    ShapeSerializable localShapeSerializable = new ShapeSerializable(localRectangle2D);
    paramObjectOutputStream.writeObject(localShapeSerializable);
    BufferedImage localBufferedImage = this.texturePaint.getImage();
    if (localBufferedImage != null)
    {
      ImageSerializable localImageSerializable = new ImageSerializable(localBufferedImage);
      paramObjectOutputStream.writeObject(localImageSerializable);
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof TextureBackground))
      return false;
    TextureBackground localTextureBackground = (TextureBackground)paramObject;
    return texturePaintEquals(getTexturePaint(), localTextureBackground.getTexturePaint());
  }

  private boolean texturePaintEquals(TexturePaint paramTexturePaint1, TexturePaint paramTexturePaint2)
  {
    if ((paramTexturePaint1 == null) || (paramTexturePaint2 == null))
      return ((paramTexturePaint1 == null) && (paramTexturePaint2 == null));
    return ((ComparatorUtils.equals(paramTexturePaint1.getAnchorRect(), paramTexturePaint2.getAnchorRect())) && (ComparatorUtils.imageEquals(paramTexturePaint1.getImage(), paramTexturePaint2.getImage())));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    if (this.texturePaint != null)
      ChartUtils.image2JS(localJSONObject, this.texturePaint.getImage());
    return localJSONObject;
  }

  public String getBackgroundType()
  {
    return "TextureBackground";
  }
}